<template>
	<view class="audioPlay">
		<view>当前状态：{{msg}}</view>
		<view style="position: fixed;bottom: 0;width: 100%;" flex="main:justify">
			
			<button class="flex_1" @click="startRecord">开始录音</button>
			<button class="flex_1" @tap="endRecord">停止录音</button>
			<!-- <button @tap="playVoice">播放录音</button> -->
		</view>
	</view>
</template>
<script>
	const recorderManager = uni.getRecorderManager();
	const innerAudioContext = uni.createInnerAudioContext();
	export default {
		data() {
			return {
				recorderManager: {},
				innerAudioContext: {},
				text: 'uni-app',
				voicePath: '',
				msg: ''
			}
		},
		onLoad() {
			innerAudioContext.autoplay = true;
			console.log("uni.getRecorderManager()", uni.getRecorderManager())
			console.log("uni.createInnerAudioContext()", uni.createInnerAudioContext())
			let self = this;
			recorderManager.onStop(function(res) {
				console.log('recorder stop' + JSON.stringify(res));
				self.voicePath = res.tempFilePath;
			});
		},
		methods: {
			startRecord() {
				console.log('开始录音');
				this.msg = '开始录音'
				recorderManager.start();
			},
			endRecord() {
				console.log('录音结束');
				this.msg = '录音结束'
				recorderManager.stop();
			},
			playVoice() {
				console.log('播放录音');
				this.msg = '播放录音'
				if (this.voicePath) {
					console.log(this.voicePath);
					innerAudioContext.src = this.voicePath;
					innerAudioContext.play();
				}
			},

		},
		beforeDestroy() {
			console.log('录音销毁');
			this.recorderManager = null
		}
	}
</script>

<style lang="scss">
	
</style>